/**
 *  *2010-7-12 下午11:53:22  add 
 */
package com.base.myproject.client.busi.ccode;

import com.base.myproject.client.system.Env;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.tools.CommandSyncsql;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * 重新将ccode的password变为空
 * 
 * @author Administrator 2010-7-12 javazgw@gmail.com
 */
public class ResetCcodePass {
	FormPanel formpanel = new FormPanel();
	Dialog d = new Dialog();
	TextField<String> s_cardnoT = new TextField<String>();

	public ResetCcodePass() {
		init();
	}

	private void init() {

	}

	public void showDialog() {

		d.setWidth("100");
		// d.setTitle("重置会员密码");
		d.setHeading("重置会员密码");
		FormData fd = new FormData("-20");
		// formpanel.setLabelWidth(75);
		// formpanel.setFieldWidth(100);
		formpanel.setHeaderVisible(false);
		formpanel.setBodyBorder(false);
		formpanel.setBorders(false);
		s_cardnoT.setFieldLabel("会员卡号");
		formpanel.add(s_cardnoT, fd);

	

		formpanel.add(new Html(
				"操作提示：你将清空会员的密码，此操作是不能回退的。除非你知道你在做什么，否则不要点击确认按钮。"), fd);
		formpanel.add(new Html(
		"重置密码后，你可以通过“修改会员密码” 给会员设置新密码。"), fd);
		d.setButtons(Dialog.OKCANCEL);

		d.getButtonById(Dialog.CANCEL)
				.addListener(Events.Select, getListener());

		d.getButtonById(Dialog.OK).addListener(Events.Select, getListener());
		d.add(formpanel);
		d.setSize(360, 300);
		d.show();

	}

	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {
							if (be.getSource().equals(
									d.getButtonById(Dialog.OK))) {
								if (s_cardnoT.getValue() == null) {
									MessageBox.alert("错误", "请录入会员卡号", null);
								} else {
									resetpass(s_cardnoT.getValue());
								}
							}
							else if (be.getSource().equals(
									d.getButtonById(Dialog.CANCEL)))
							{
								close();
								
							}
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}

	void resetpass(String pass) {

		CommandSyncContainer list = new CommandSyncContainer();
		CommandSyncsql commandsql = new CommandSyncsql();
		commandsql.getV().add(
				"update ccode set password = null ,updatedate=getdate(),updatebcode='"+Env.getEnvbcode()+"' where s_cardno='" + pass
						+ "'");
		list.add(commandsql);
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.SendCommandSync(list,
				new AsyncCallback<CommandSyncReturnObject>() {
					public void onFailure(Throwable caught) {
						Window.alert("网络连接不稳定，请稍后重试！");
					}

					public void onSuccess(CommandSyncReturnObject cyro) {

						if (cyro.isB()) {
							Info.display("", "密码重置成功", "");
							close();
							// close();
						} else {
							MessageBox.alert("错误", "密码重置失败！"
									+ cyro.getMessage(), null);
						}
					}
				});
		//d.getButtonById(Dialog.YES).setEnabled(false);
	
	}
	
	void close()
	{

			d.hide();
		
	}

}
